package com.dk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dk.VO.TbCarsVO;
import com.dk.VO.TbOrderVO;
import com.dk.bo.AlipayOrderBO;
import com.dk.mapper.TbOrderMapper;
import com.dk.pojo.TbOrder;
import com.dk.service.TbCarsService;
import com.dk.service.TbOrderService;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
public class TbOrderServiceImpl implements TbOrderService {

    @Resource
    private TbOrderMapper orderMapper;
    @Resource
    private TbCarsService carsService;

    @Override
    public List<TbOrderVO> getOrdersByUserId(Long userId) {
        List<TbOrderVO> tbOrders = orderMapper.getOrdersByUserId(userId);
        return tbOrders;
    }

    @Override
    public List<TbOrderVO> getSoldCars(Long userId) {
        QueryWrapper<TbOrder> wrapper = new QueryWrapper<>();
        wrapper.eq("seller_id", userId);
        return orderMapper.getSoldCars(userId);
    }

    @Override
    public TbOrder selectOrderBy(String out_trade_no) {
        return orderMapper.selectByOrderNum(out_trade_no);
    }

    @Override
    public void updateTradeNoAndPaymentTimeByOrderNum(String out_trade_no, String trade_no) {
        orderMapper.updateTradeNoAndPaymentTimeByOrderNum(out_trade_no, trade_no, new Date());
    }

    @Override
    public void updateStatusByOrderNum(String refundOrderNum, String s) {
        orderMapper.updateStatusByOrderNum(refundOrderNum, s);
    }

    @Override
    public void updateStatusByTradeNo(String trade_no, String s) {
        orderMapper.updateStatusByTradeNo(trade_no, s);
    }

//    @Transactional(rollbackFor = Exception.class)
    //配置事务回滚策略
//    @GlobalTransactional(rollbackFor = Exception.class)
    @Override
    public AlipayOrderBO createOrder(Long userid, String phone, Long cid, Long num) {

        TbCarsVO carsVO = carsService.getAll(cid);

        String orderId = RandomStringUtils.randomNumeric(16);//随机订单ID
        String orderNum = RandomStringUtils.randomNumeric(16);//随机订单号

        //订金,2000元
        BigDecimal payment = new BigDecimal(num);

        TbOrder order = new TbOrder();
        order.setOrderId(Long.valueOf(orderId));  //订单ID
        order.setSellerId(carsVO.getUserId());  //卖家ID
        order.setCarId(cid);    //车辆ID
        order.setUserId(userid);  //用户ID
        order.setPayment(payment); //订金
        order.setPaymentType("支付宝");    //支付方式
        order.setStatus("1");   //支付状态，默认未付款“1”
        order.setCreateTime(new Date());    //订单创建时间
        order.setUpdateTime(new Date());    //订单更新时间
        order.setReceiverMobile(phone); // 买家手机号
        order.setTotalFee(carsVO.getPrice());
        order.setExpire("1800");
        order.setInvoiceType("普通发票");
        order.setOrderNum(orderNum);
        order.setTitle(carsVO.getTitle());
        orderMapper.insert(order);

        AlipayOrderBO orderBo = new AlipayOrderBO();
        orderBo.setOutTrandNo(orderNum);//订单号
        orderBo.setOrderName(carsVO.getTitle());//订单名称
        orderBo.setMoney(carsVO.getPrice().toString());//付款金额
        orderBo.setCarsDesc("汽车");//车辆描述

        return orderBo;
    }
}
